package com.bc.web.controller.org;


import java.awt.Color;
import java.io.File;
import java.io.IOException;

import com.bc.web.config.R;
import com.bc.web.config.SystemEnum.FilePath;
import com.bc.web.controller.PictureProcess;
import com.bc.web.model.Merch;
import com.bc.web.model.MerchUser;
import com.bc.web.model.Org;
import com.bc.web.model.OrgUser;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSFile;
import com.fs.common.util.FSZIP;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

public class MerchController extends FSController{
	
	public void search(){
		OrgUser orgUser = (OrgUser) getSession().getAttribute("orgUser");
		Page<Merch> items = Merch.dao.search(super.getSearchCondition(),orgUser.getOrgCode());
		setPageResultToRequest("items", items);
		render("merch/merchList.html");
	}
	
	public void lookup() {
		OrgUser orgUser = (OrgUser) getSession().getAttribute("orgUser");
		Page<Merch> items = Merch.dao.search(super.getSearchCondition(),orgUser.getOrgCode());
		setPageResultToRequest("items", items);
		render("merch/merchLookup.html");
	}
	
	
	/**
	 * 商户详细信息
	 */
	public void view(){
		OrgUser orgUser = (OrgUser) getSession().getAttribute("orgUser");
		Merch merch = Merch.dao.findById(getPara("id"));
		//获取图片列表
		String merchDataPath = FilePath.MerchData.getValue() + File.separator + merch.getCode();
		File merchDataFileDir = new File(merchDataPath);
		if(!merchDataFileDir.exists()){
			merchDataFileDir.mkdirs();
		}
		
		//String[] temp = merchDataFileDir.list();
//		if(temp != null && temp.length > 0){
//			String[] fileNames = new String[temp.length];
//			for (int i = 0; i < temp.length; i++) {
//				fileNames[i] = File.separator + merch.getCode() + File.separator + temp[i];
//			}
//			setAttr("fileNameList", fileNames);
//		}
		
		String[] fileNameList = merchDataFileDir.list();
		setAttr("fileNameList", fileNameList);
		setAttr("org", Org.dao.selectByOrgCode(orgUser.getOrgCode()));
		setAttr("merch", merch);
		render("merch/merchView.html");
	}
	
	public void showFile(){
		String fileName = getPara("fileName");
		String merchCode = getPara("merchCode");
		String filePath = FilePath.MerchData.getValue() + merchCode + File.separator + fileName;
		if(StrKit.notBlank(fileName)){
			super.renderStream(filePath); 
		}else{
			renderNull();
		}
	}
	
	/**
	 * 下载资料
	 * @throws Exception
	 */
	public void downloadFile() throws Exception{
		String merchCode = getPara("merchCode");
		String merchDataPath = FilePath.MerchData.getValue() + File.separator + merchCode;
		String zipDir = FilePath.Zip.getValue();
		String zipName = "商户资料(" + merchCode + ").zip";
		FSFile.mkdir(zipDir);
		FSZIP.zip(merchDataPath, zipDir + zipName);
		renderFile(new File(zipDir + zipName));
	}
	
	
private static final String REPORT_PATH = PathKit.getWebRootPath()+File.separator +"payimg"+File.separator; //SystemEnum.FilePath.ReportTmp.getValue();
		
	public void downloadQcodeJpg() throws IOException{
		String merchUser = getPara("merch");
		Merch merch = Merch.dao.selectByMerchCode(merchUser);
		File file = null;
		String targetPath = REPORT_PATH + merchUser+ ".jpg";
		
		String tmp_targetPath = REPORT_PATH + merchUser+ ".jpg";
		
		String logoPath = REPORT_PATH + "logook.png";
		file = new File(targetPath);
		String srcImgPath =REPORT_PATH + "backgroup.jpg";
		if (file == null || !file.exists() || !file.isFile()) {
			PictureProcess.CreateQrcodeToImgPath(R.QCodeUrl +merchUser, targetPath,logoPath,16,16,false);
			// 二维码当水印添加到图片
			PictureProcess.markImageByIcon(targetPath, srcImgPath, targetPath, 1 , 165);
			PictureProcess.createStringMark(tmp_targetPath, merch.getName(), Color.WHITE, 70, targetPath, 1800, 600);
			file = new File(targetPath);
			renderFile(file);
		} else {
			renderFile(file);
		}
	}
}
